package com.block.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.block.common.constant.RedisKeyConstant;
import com.block.common.exception.NotifyExceptionService;
import com.block.db.entity.SourceDataCountInfo;
import com.block.db.mapper.SourceDataCountInfoMapper;
import com.block.common.model.dto.SourceCountDTO;
import com.block.common.service.ISourceDataCountInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 线索数据统计 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-12-27
 */
@Service
public class SourceDataCountInfoServiceImpl extends ServiceImpl<SourceDataCountInfoMapper, SourceDataCountInfo> implements ISourceDataCountInfoService {

    @Autowired
    NotifyExceptionService notifyExceptionService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

//    @Async
    @Override
    @Transactional
    public void saveSourceCountInfo(SourceCountDTO sourceCountDTO){
        try {
            sourceCountDTO.setDateInfo(LocalDate.now().toString());

            SourceDataCountInfo dataCountInfo = this.baseMapper.selectByDateInfoAndChannelCode(sourceCountDTO.getChannelCode(), sourceCountDTO.getDateInfo());
            if (ObjectUtil.isEmpty(dataCountInfo)){
                dataCountInfo = this.initSourceCountInfo(sourceCountDTO);
                if (ObjectUtil.isEmpty(dataCountInfo)){
                    throw new RuntimeException(StrUtil.format("新增线索统计信息异常【sourceCountDTO】{}", JSON.toJSONString(sourceCountDTO)));
                }
            }

            SourceDataCountInfo updateInfo = this.getUpdateEntity(sourceCountDTO, dataCountInfo);
            updateInfo.setId(dataCountInfo.getId());
            boolean update = this.updateById(updateInfo);
            if (!update){
                throw new RuntimeException(StrUtil.format("修改线索统计信息异常【sourceCountDTO】{}", JSON.toJSONString(sourceCountDTO)));
            }

            String dateInfo = LocalDate.now().toString();

            // 更新通知成功的缓存信息
            if (ObjectUtil.isNotEmpty(sourceCountDTO.getApproveSuccessNumber())){
                String successNumberKey = StrUtil.format(RedisKeyConstant.SOURCE_SHOULD_SUCCESS_NUMBER, dateInfo, sourceCountDTO.getChannelCode());
                stringRedisTemplate.opsForValue().set(successNumberKey, updateInfo.getApproveSuccessNumber()+"", 24, TimeUnit.HOURS);
            }

            if (ObjectUtil.isNotEmpty(sourceCountDTO.getNoticeNumber())){
                String noticeNumberKey = StrUtil.format(RedisKeyConstant.SOURCE_NOTICE_SUCCESS_NUMBER, dateInfo, sourceCountDTO.getChannelCode());
                stringRedisTemplate.opsForValue().set(noticeNumberKey, updateInfo.getNoticeNumber()+"", 24, TimeUnit.HOURS);
            }

        }catch (Exception exception){
            exception.printStackTrace();
            notifyExceptionService.notifyNoticeInfo("SOURCE_COUNT_EXC", "线索统计异常", StrUtil.format("【sourceCountDTO】{}", JSON.toJSONString(sourceCountDTO)), exception);
        }
    }

    private SourceDataCountInfo getUpdateEntity(SourceCountDTO sourceCountDTO, SourceDataCountInfo sourceDataCountInfo) {
        SourceDataCountInfo updateEntity = new SourceDataCountInfo();

        if (ObjectUtil.isNotEmpty(sourceCountDTO.getCheckNumber())){
            updateEntity.setCheckNumber(sourceCountDTO.getCheckNumber() + sourceDataCountInfo.getCheckNumber());
        }

        if (ObjectUtil.isNotEmpty(sourceCountDTO.getCheckPassNumber())){
            updateEntity.setCheckPassNumber(sourceCountDTO.getCheckPassNumber() + sourceDataCountInfo.getCheckPassNumber());
        }

        if (ObjectUtil.isNotEmpty(sourceCountDTO.getRegisterNumber())){
            updateEntity.setRegisterNumber(sourceCountDTO.getRegisterNumber() + sourceDataCountInfo.getRegisterNumber());
        }


        if (ObjectUtil.isNotEmpty(sourceCountDTO.getRegisterPassNumber())){
            updateEntity.setRegisterPassNumber(sourceCountDTO.getRegisterPassNumber() + sourceDataCountInfo.getRegisterPassNumber());
        }

        if (ObjectUtil.isNotEmpty(sourceCountDTO.getOpenApproveNumber())){
            updateEntity.setOpenApproveNumber(sourceCountDTO.getOpenApproveNumber() + sourceDataCountInfo.getOpenApproveNumber());
        }

        if (ObjectUtil.isNotEmpty(sourceCountDTO.getClickApproveNumber())){
            updateEntity.setClickApproveNumber(sourceCountDTO.getClickApproveNumber() + sourceDataCountInfo.getClickApproveNumber());
        }

        if (ObjectUtil.isNotEmpty(sourceCountDTO.getApproveSuccessNumber())){
            updateEntity.setApproveSuccessNumber(sourceCountDTO.getApproveSuccessNumber() + sourceDataCountInfo.getApproveSuccessNumber());
        }

        if (ObjectUtil.isNotEmpty(sourceCountDTO.getNoticeNumber())){
            updateEntity.setNoticeNumber(sourceCountDTO.getNoticeNumber() + sourceDataCountInfo.getNoticeNumber());
        }

        if (ObjectUtil.isNotEmpty(sourceCountDTO.getNoticeSuccessNumber())){
            updateEntity.setNoticeSuccessNumber(sourceCountDTO.getNoticeSuccessNumber() + sourceDataCountInfo.getNoticeSuccessNumber());
        }

        return updateEntity;
    }

    public SourceDataCountInfo initSourceCountInfo(SourceCountDTO sourceCountDTO){
        SourceDataCountInfo dataCountInfo = new SourceDataCountInfo();
        dataCountInfo.setChannelCode(sourceCountDTO.getChannelCode());
        dataCountInfo.setChannelName(sourceCountDTO.getChannelName());
        dataCountInfo.setDateInfo(sourceCountDTO.getDateInfo());

        boolean save = this.save(dataCountInfo);
        if (!save){
            return null;
        }
        return this.getById(dataCountInfo.getId());
    }

}
